package com.doeis.uwork.module;

import java.util.ArrayList;
import java.util.List;

import com.doeis.minaserver.NettyServer;
import com.doeis.minaserver.container.BasicModule;
import com.doeis.minaserver.module.ClientConnectionManager;
import com.doeis.minaserver.packet.Packet;
import com.doeis.minaserver.session.ClientConnection;
import com.doeis.uwork.common.PacketUtil;
import com.doeis.uwork.dao.GroupMsgMongoDao;
import com.doeis.uwork.dao.OffPacketMongoDao;
import com.doeis.uwork.entity.mongo.OffPacket;
import com.doeis.uwork.packet.GroupMsgPacket;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.mongodb.BasicDBObject;

public class RollPolingModule extends BasicModule {

//	private PolingThread polingThread = null;
	private ClientConnectionManager clientConnectionManager = null;
	private boolean destory = false;
	
	public RollPolingModule() {
		super(" roll polling ");
	}
	

	@Override
	public void initialize(NettyServer server) {
		this.server = server;
	}
	
	@Override
	public void start() {
		this.clientConnectionManager = server.getClientConnectionManager();
		
		destory = false;
//		polingThread = new PolingThread();
//		polingThread.start();
	}
	
	@Override
	public void stop() {
		destory = true;
	}
	
	

	private long sleeptime = 10 *1000 ;
	class PolingThread extends Thread{
		
		OffPacketMongoDao packetMongoDao;
		GroupMsgMongoDao msgMongoDao ;
		public PolingThread(){
			Injector injector = Guice.createInjector();
			packetMongoDao = injector.getInstance(OffPacketMongoDao.class);
			msgMongoDao = injector.getInstance(GroupMsgMongoDao.class);
		}
		
//		@Override
//		public void run() {
//			while(!destory){
//				try {
//					Thread.sleep(sleeptime);
//					
//					List<OffPacket> listOffPacket = null;
//					List<String> listMongo = MongoManage.getDatabaseNames();
//					if(listMongo==null || listMongo.size()==0 ){
//						continue ;
//					}
//					
//					ClientConnection clientConnection  = null;
//					
//					BasicDBObject query = new BasicDBObject(); 
//					
//					BasicDBObject parameter = new BasicDBObject();
//					parameter.append("$lte", 5 );//慢的话，这个字段可以建立索引
//					query.put("sendCount", parameter);
//					
//					for(String dbname : listMongo ){
//						
//						listOffPacket = packetMongoDao.query(dbname, OffPacket.class, query);
//						if(listOffPacket==null || listOffPacket.size()==0 ){
//							continue ;
//						}
//						
//						System.err.println("listOffPacket.size"+listOffPacket.size());
//						
//						long currentTime = System.currentTimeMillis();
//						for(OffPacket offPacket : listOffPacket ){
//							clientConnection = clientConnectionManager.getAuthConnection(offPacket.getUserId());
//							if(clientConnection==null || clientConnection.isClosed() ){
//								continue ;
//							}
//							
//							if( (currentTime-offPacket.getCreated_at().getTime()) < sleeptime ){
//								continue ;
//							}
//							
//							if(offPacket.getSendCount() >= 5 ){
//								offPacket.setSendCount(0);
//								packetMongoDao.update(dbname, offPacket);
//								clientConnection.close();
//								continue ;
//							}
//							
//							if(offPacket.getCmd() == GroupMsgPacket.NOTIFY_MSG_ADD){// A > 服务器  然后 服务器 向A相应
//								Packet packet = PacketUtil.clonePacket(offPacket);
//								clientConnection.deliver(packet);
//								
//								offPacket.setSendCount(offPacket.getSendCount()+1);
//								packetMongoDao.update(dbname, offPacket);
//							}else if(offPacket.getCmd() == GroupMsgPacket.CMD_MSG_ADD_OTHER){//服务器 > B 发送推送信息
//								Packet packet = PacketUtil.clonePacket(offPacket);
//								packet.setCmd( GroupMsgPacket.CMD_MSG_ADD_OTHER_REPEAT);
//								clientConnection.deliver(packet);
//									offPacket.setSendCount(offPacket.getSendCount()+1);
//									packetMongoDao.update(dbname, offPacket);
//							}
//						}
//					}
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		}
		
		
		@Override
		public void run() {
			while(!destory){
				try {
					Thread.sleep(sleeptime);
					List<ClientConnection> listValue = new ArrayList<ClientConnection>(clientConnectionManager.getAuthConnections().values());
					if(listValue.size() == 0 ){
						continue ;
					}
					
					System.err.println("   "+listValue.size());
					
					List<OffPacket> listOffPacket = null;
					
					BasicDBObject query = new BasicDBObject(); 
					BasicDBObject parameter = new BasicDBObject();
					parameter.append("$lte", 5 );//慢的话，这个字段可以建立索引
					query.put("sendCount", parameter);
					
					for(ClientConnection clientConnection : listValue ){
						
						if(clientConnection.isClosed() ){
							continue ;
						}
						
						query.put("to", clientConnection.getUserid());
						
						listOffPacket = packetMongoDao.query(clientConnection.getSwitchboard_id(), OffPacket.class, query);
						if(listOffPacket==null ){
							continue ;
						}
						
						System.err.println(clientConnection.getUserid()+" = "+listOffPacket.size());
						
						long currentTime = System.currentTimeMillis();
						for(OffPacket offPacket : listOffPacket ){
							
							if( (currentTime-offPacket.getCreated_at().getTime()) < sleeptime ){
								continue ;
							}
							
							if(offPacket.getSendCount() >= 5 ){
								offPacket.setSendCount(0);
								packetMongoDao.update(clientConnection.getSwitchboard_id(), offPacket);
								clientConnection.close();
								continue ;
							}
							
							if(offPacket.getCmd() == GroupMsgPacket.NOTIFY_MSG_ADD){// A > 服务器  然后 服务器 向A相应
								Packet packet = PacketUtil.clonePacket(offPacket);
								clientConnection.deliver(packet);
								
								offPacket.setSendCount(offPacket.getSendCount()+1);
								packetMongoDao.update(clientConnection.getSwitchboard_id(), offPacket);
							}else if(offPacket.getCmd() == GroupMsgPacket.CMD_MSG_ADD_OTHER){//服务器 > B 发送推送信息
								Packet packet = PacketUtil.clonePacket(offPacket);
								packet.setCmd( GroupMsgPacket.CMD_MSG_ADD_OTHER_REPEAT);
								clientConnection.deliver(packet);
								offPacket.setSendCount(offPacket.getSendCount()+1);
								packetMongoDao.update(clientConnection.getSwitchboard_id(), offPacket);
							}
						}
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
	
	

}
